package com.cq.digitalemployee.shiro.config;

import com.cq.digitalemployee.shiro.token.AuthToken;
import com.cq.digitalemployee.shiro.token.TokenEntity;
import com.cq.digitalemployee.shiro.token.service.TokenService;
import com.cq.digitalemployee.user.entity.po.SysPermission;
import com.cq.digitalemployee.user.entity.po.SysRole;
import com.cq.digitalemployee.user.entity.po.SysUser;
import com.cq.digitalemployee.user.entity.vo.SysUserVo;
import com.cq.digitalemployee.user.service.PermissionService;
import com.cq.digitalemployee.user.service.RoleService;
import com.cq.digitalemployee.user.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author zzz
 */
@Component
public class UserRealm extends AuthorizingRealm {

    private static final Logger logger = LoggerFactory.getLogger(UserRealm.class);

    @Autowired
    SysUserService sysUserService;

    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;

    @Autowired
    TokenService tokenService;

    /**
     * 执行授权逻辑
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.debug("======执行授权======");

        TokenEntity tokenEntity = (TokenEntity) principalCollection.getPrimaryPrincipal();
        Set<SysRole> roles = tokenEntity.getRoles();
        Set<SysPermission> permissions = tokenEntity.getPermissions();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Collection<String> rolesCollection = new HashSet<>();
        Collection<String> permissionCollection = new HashSet<>();
        for (SysRole role : roles) {
            rolesCollection.add(role.getName());
        }
        for (SysPermission permission : permissions) {
            permissionCollection.add(permission.getCode());
        }
        info.addRoles(rolesCollection);
        info.addStringPermissions(permissionCollection);
        return info;
    }

    /**
     * 执行认证逻辑
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        logger.debug("======执行认证======");

        String token = (String) authenticationToken.getPrincipal();

        TokenEntity authToken = tokenService.getToken(token);
        if (authToken == null) {
            throw new IncorrectCredentialsException("token已失效");
        }

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(authToken, authToken.getToken(), this.getName());

        return info;
    }
}
